package com.tql.pandian.service.impl;

import com.ruoyi.common.core.constant.CommonUtil;
import com.ruoyi.common.core.utils.CalculatedValueUtil;
import com.ruoyi.common.datasource.annotation.Master;
import com.ruoyi.common.datasource.annotation.Slave;
import com.tql.pandian.entity.TqlHllShopBillFoodReportDayEntity;
import com.tql.pandian.entity.TqlStapOutChange;
import com.tql.pandian.entity.TqlStapOutChangeBomInfo;
import com.tql.pandian.entity.dto.TqlHllShopBillFoodDTO;
import com.tql.pandian.mapper.TqlHllShopBillFoodMapper;
import com.tql.pandian.service.ITqlHllShopBillFoodService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class TqlHllShopBillFoodService implements ITqlHllShopBillFoodService {

    @Autowired
    private TqlHllShopBillFoodMapper tqlHllShopBillFoodMapper;

    /**
     * 插入 tql_hll_shop_bill_food_reportday 表中
     * @return
     */
    @Override
    @Transactional
    public int insertBillFoodReportDay() {
        List<TqlHllShopBillFoodReportDayEntity> list = new ArrayList<>();
        Calendar calendar = Calendar.getInstance();
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        int reportDate = Integer.parseInt(sdf.format(date));
        int month = calendar.get(Calendar.MONTH) +1;
        /************  设置表名  ************/
        String shopBillFoodReportDay = CommonUtil.TableName_ShopBillFoodReportDayDetail + month;

        String shopBillFood = CommonUtil.TableName_ShopBillFoodDetail + month;
        /************   插入表字段  ************/
        List<TqlHllShopBillFoodDTO> foodList = tqlHllShopBillFoodMapper.selectShopBillFood(shopBillFood);
        foodList.forEach(t-> {
            TqlHllShopBillFoodReportDayEntity aa = new TqlHllShopBillFoodReportDayEntity();
            aa.setFoodName(t.getFoodName());
            aa.setFoodCode(t.getFoodCode());
            aa.setFoodCount(t.getFoodCount());
            aa.setReportDate(t.getReportDate());
            aa.setCreatetime(t.getCreatetime());
            aa.setFoodRealPrice(t.getFoodRealAmount());
            aa.setShopId(t.getShopID());
            aa.setCreatetime(date);
            list.add(aa);
        });
        int count = 0;
        if (!list.isEmpty()){
            count = tqlHllShopBillFoodMapper.insertShopBillFoodReportDay(shopBillFoodReportDay, list);
            tqlHllShopBillFoodMapper.updateShopBillFood(shopBillFood, foodList);
        }
        return count;
    }
    @Override
    public void insertStapOutChange1(int month, List<String> relDeptIds) {
        if(0==month)
        {
            Calendar now = Calendar.getInstance();
            month = now.get(Calendar.MONTH) + 1;
        }

        //只获取已经关联的门店
      //  List<String> relDeptIds = this.selectRelDeptIds();
        //获取未更新到 tql_stap_out_change 表中得数据
        String shopBillFoodReportdayTableName = CommonUtil.TableName_ShopBillFoodReportDayDetail + month;
        List<TqlStapOutChange> entityList= this.getBillFoodReportList(shopBillFoodReportdayTableName,relDeptIds);
        List<TqlStapOutChange> newEntityList = new ArrayList<>();
        if (!entityList.isEmpty()) {
            for (TqlStapOutChange outChange : entityList) {
                List<TqlStapOutChangeBomInfo> bomInfoList = this.selectShopBillFoodReportDayBomInfo(outChange.getShopId(), outChange.getFoodCode());
                if (bomInfoList.isEmpty()) {
                    continue;
                }
                for (TqlStapOutChangeBomInfo bomInfo : bomInfoList) {
                    outChange.setDeptId(bomInfo.getDeptId());
                    outChange.setStapCode(bomInfo.getStapCode());
                    outChange.setStapName(bomInfo.getStapName());
                    BigDecimal sumquota=CalculatedValueUtil.multiplyB(bomInfo.getQuota(),outChange.getFoodCount());
                    outChange.setSumquota(sumquota);
                    BigDecimal sumcost=CalculatedValueUtil.multiplyB(bomInfo.getCost(),outChange.getFoodCount());
                    outChange.setSumcost(sumcost);
                    outChange.setDeptCode(bomInfo.getDeptCode());
                    newEntityList.add(outChange);
                }
            }

            insertAndUpdate(month,shopBillFoodReportdayTableName,newEntityList);
        }

    }

    @Override
    @Slave
    public List<TqlStapOutChange> getBillFoodReportList1(int month, String shopID, Long std, Long etd) {
        String tableName = CommonUtil.TableName_ShopBillFoodReportDayDetail + month;


            return    tqlHllShopBillFoodMapper.selectShopBillFoodReportDay1(tableName,shopID,std,etd);

    }

    @Override
    @Slave
    public void clearOutByShopIdAndReportDate(int month, String shopID, Long std, Long etd) {
        String tabName=CommonUtil.TQL_STAP_OUT_CHANGE+month;
        tqlHllShopBillFoodMapper.clearOutByShopIdAndReportDate(tabName,shopID,std,etd);

    }

    @Override
    @Slave
    public void insertAndUpdate1(int month, String shopBillFoodReportdayTableName, List<TqlStapOutChange> newEntityList) {
        String stapOutChangeTableName = CommonUtil.TQL_STAP_OUT_CHANGE + month;

        if(!CollectionUtils.isEmpty(newEntityList))
            tqlHllShopBillFoodMapper.insertStapOutChange(stapOutChangeTableName, newEntityList);
        if(!CollectionUtils.isEmpty(newEntityList))
            tqlHllShopBillFoodMapper.updateShopBillFoodReportDay1(shopBillFoodReportdayTableName, newEntityList);
    }


    @Override
    @Transactional
    public void insertStapOutChange(int month) {

        if(0==month)
        {
            Calendar now = Calendar.getInstance();
            month = now.get(Calendar.MONTH) + 1;
        }

        //只获取已经关联的门店
        List<String> relDeptIds = this.selectRelDeptIds();
        //获取未更新到 tql_stap_out_change 表中得数据
        String shopBillFoodReportdayTableName = CommonUtil.TableName_ShopBillFoodReportDayDetail + month;
        List<TqlStapOutChange> entityList= this.getBillFoodReportList(shopBillFoodReportdayTableName,relDeptIds);
        List<TqlStapOutChange> newEntityList = new ArrayList<>();
        if (!entityList.isEmpty()) {
            for (TqlStapOutChange outChange : entityList) {
                List<TqlStapOutChangeBomInfo> bomInfoList = this.selectShopBillFoodReportDayBomInfo(outChange.getShopId(), outChange.getFoodCode());
                if (bomInfoList.isEmpty()) {
                    continue;
                }
                for (TqlStapOutChangeBomInfo bomInfo : bomInfoList) {
                    outChange.setDeptId(bomInfo.getDeptId());
                    outChange.setStapCode(bomInfo.getStapCode());
                    outChange.setStapName(bomInfo.getStapName());
                    BigDecimal sumquota=CalculatedValueUtil.multiplyB(bomInfo.getQuota(),outChange.getFoodCount());
                    outChange.setSumquota(sumquota);
                    BigDecimal sumcost=CalculatedValueUtil.multiplyB(bomInfo.getCost(),outChange.getFoodCount());
                    outChange.setSumcost(sumcost);
                    outChange.setDeptCode(bomInfo.getDeptCode());
                    newEntityList.add(outChange);
                }
            }

            insertAndUpdate(month,shopBillFoodReportdayTableName,newEntityList);
        }

    }

    @Slave
    @Override
    public void insertAndUpdate(int month, String shopBillFoodReportdayTableName, List<TqlStapOutChange> newEntityList) {
        String stapOutChangeTableName = CommonUtil.TQL_STAP_OUT_CHANGE + month;

        if(!CollectionUtils.isEmpty(newEntityList))
        tqlHllShopBillFoodMapper.insertStapOutChange(stapOutChangeTableName, newEntityList);
        if(!CollectionUtils.isEmpty(newEntityList))
        tqlHllShopBillFoodMapper.updateShopBillFoodReportDay(shopBillFoodReportdayTableName, newEntityList);
    }

    @Slave
    @Override
    public   List<TqlStapOutChange> getBillFoodReportList(String  shopBillFoodReportdayTableName, List<String> relDeptIds) {
        List<TqlStapOutChange> entityList =  tqlHllShopBillFoodMapper.selectShopBillFoodReportDay(shopBillFoodReportdayTableName, relDeptIds);
        return entityList;
    }

    @Override
    @Master
    public List<String> selectRelDeptIds() {
        return tqlHllShopBillFoodMapper.selectRelDeptIds();
    }

    @Override
    @Master
    public List<TqlStapOutChangeBomInfo> selectShopBillFoodReportDayBomInfo(Long shopId, String foodCode) {
        return tqlHllShopBillFoodMapper.selectShopBillFoodReportDayBomInfo(shopId, foodCode);
    }

    @Override
    @Master
    public List<String> findEnableFoodCode(String shopId) {
        return tqlHllShopBillFoodMapper.findEnableFoodCode(shopId);
    }


}
